'use strict';

/* App Module */

var betsApp = angular.module('betsApp', [
                                         'angularMoment',
                                         'ngRoute',
                                         'ui.bootstrap',
                                         'ui.bootstrap.modal',
                                         'betControllers',
                                         'betappFilters',
                                         'ui.bootstrap.datetimepicker',
                                         'trNgGrid'
                                     ]);

/* Filters */

var betappFilters= angular.module('betappFilters', []);

betappFilters.filter('checkmark', function() {
  return function(input) {
    return input ? '\u2713' : '\u2718';
  };
});

betappFilters.filter('ownerFilter', function() {
    return function(input) {
        return input ==null ? 'EddeVedde\u2122' : input;
    };
});

betappFilters.filter('uncerScoreToSpace', function() {
    return function(input) {
        return input.replace(/_/g,' ');
    };
});

betappFilters.filter('catimgfilter', function() {
    return function(input) {
        if(input=='No category')
          return "img/null.jpg";  
        return "img/"+input.toUpperCase()+".jpg";
    };
});


betappFilters.filter('isNaN',function() {
    return function(input) {
        if(isNaN(input))
          return "No odds available (No bets yet)";  
        if(input=='NaN' || input=='Infinity')
          return "No odds available (No bets yet)";  
        return "odds " +input;
    };
});

betappFilters.filter('league', function() {
    return function(input) {

        var comps=[{"id":"1005","name":"UEFA Champions League","region":"International"},
  {"id":"1007","name":"UEFA Europa League","region":"International"},
  {"id":"1093","name":"Bundesliga","region":"Austria"},
  {"id":"1102","name":"Pro League","region":"Belgium"},
  {"id":"1184","name":"Czech Liga","region":"Czech Republic"},
  {"id":"1198","name":"Fa Cup","region":"England"},
  {"id":"1204","name":"Premier League","region":"England"},
  {"id":"1205","name":"Championship","region":"England"},
  {"id":"1221","name":"Ligue 1","region":"France"},
  {"id":"1229","name":"Bundesliga","region":"Germany"},
  {"id":"1232","name":"Super League","region":"Greece"},
  {"id":"1269","name":"Serie A","region":"Italy"},
  {"id":"1322","name":"Eredivisie","region":"Netherlands"},
  {"id":"1352","name":"Primeira Liga","region":"Portugal"},
  {"id":"1397","name":"Copa Del Rey","region":"Spain"},
  {"id":"1399","name":"Primera Division","region":"Spain"},
  {"id":"1408","name":"Super League","region":"Switzerland"},
  {"id":"1425","name":"Super Liga","region":"Turkey"},
  {"id":"1428","name":"Premier League","region":"Ukraine"},
  {"id":"1457","name":"Premier League","region":"Russia"}];
        var lookup = {};
        for (var i = 0, len = comps.length; i < len; i++) {
            lookup[comps[i].id] = comps[i];
        }
        if(lookup[input])
        return lookup[input].name + " ("+lookup[input].region+")";   
        return "";     
    };
});



betsApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/events', {
        title: 'Home',
        templateUrl: 'eventlist.html',
        orderby: '-event.eventPosted',
        controller: 'EventListCtrl'
      }).
      when('/new', {
        title: 'Home',
        templateUrl: 'eventlist.html',
        orderby: '-event.eventPosted',
        controller: 'EventListCtrl'
      }).
      when('/usersettings', {
        title: 'User settings',
        templateUrl: 'usersettings.html',
        controller: 'UserSettingsCtrl'
      }).            
      when('/mine', {
        title: 'My bets',
        templateUrl: 'mybets.html',
        controller: 'MyBetsCtrl'
      }).            
      when('/myevents', {
        title: 'Events I created',
        templateUrl: 'ownevents.html',
        controller: 'OwnEventsCtrl'
      }).
      when('/events/:eventId', {
        title: 'Event details',
        templateUrl: 'eventdetail.html',
        controller: 'EventDetailCtrl'
      }).
      when('/events/decide/:eventId', {
        title: 'Decide outcome',
        templateUrl: 'decideevent.html',
        controller: 'DecideEventCtrl'
      }).      
      when('/create', {
        title: 'Create',
        templateUrl: 'create.html',
        controller: 'CreateCtrl'
      }).
      when('/newuser', {
        title: 'New user',
        templateUrl: 'newuser.html',
        controller: 'newuserCtrl'
      }).      
      when('/erro/:message', {
        title: 'Error',
        templateUrl: 'error.html',
        controller: 'error'
      }).            
      otherwise({
        redirectTo: '/events'
      });
  }]);

betsApp.run(['$location', '$rootScope', function($location, $rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
      if (current.hasOwnProperty('$$route')) {
        $rootScope.title = current.$$route.title;
        }
    });
}]);

betsApp.directive('onlyDigits', function () {
    return {
        restrict: 'A',
        require: '?ngModel',
        link: function (scope, element, attrs, ngModel) {
            if (!ngModel) return;
            ngModel.$parsers.unshift(function (inputValue) {
                var digits = inputValue.split('').filter(function (s) { return (!isNaN(s) && s != ' '); }).join('');
                ngModel.$viewValue = digits;
                ngModel.$render();
                return digits;
            });
        }
    };
});


betsApp.directive("autofill", function () {
    return {
        require: "ngModel",
        link: function (scope, element, attrs, ngModel) {
            scope.$on("autofill:update", function() {
                ngModel.$setViewValue(element.val());
            });
        }
    };
});

betsApp.directive('scroller', function () {
    return {
        restrict: 'A',
        link: function (scope, elem, attrs) {
            var rawElement = elem[0]; // new
            elem.bind('scroll', function () {
                if((rawElement.scrollTop + rawElement.offsetHeight+5) >= rawElement.scrollHeight){ //new
                    scope.$apply('loadMore()');
                }
            });
        }
    };
});



